package com.fzj.example.design.proxy.proxydynamic.jdk;

import net.sf.cglib.proxy.Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
* @Description
* @Author  fanzj
* @Date   2020/5/8 11:43 上午
* @Param
* @Return
* @Exception
*
*/
public class UserInvoicationHandler implements InvocationHandler {

    private Object target;

    public UserInvoicationHandler(Object target) {
        this.target = target;
    }

    /**
     * 获取被代理接口实例对象
     * @param <T>
     * @return
     */
    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this::invoke);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Do something before");
        Object result = method.invoke(target, args);
        System.out.println("Do something after");
        return result;
    }
}
